# encoding=utf-8_general_ci

import os.path
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web

from login import LoginHandler
from main import MainHandler
from logout import LogoutHandler
from question import QuestionsHandler
from contest import ConstHandler
from rank import RankHandler
from discuss import DiscussHandler
from admin import AdminHandler
from admin import AdminOperationHandler
from design import DesignHandler
from config import *

from tornado.options import define, options

import sys
reload(sys)
sys.setdefaultencoding('utf8')

define("port", default=8080, help="run on the given port", type=int)

class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        if not self.get_secure_cookie('user'):
            self.redirect("/login")
        else:
            self.redirect("/main")

application = tornado.web.Application([
    	(r"/", IndexHandler),
        (r"/login", LoginHandler),
        (r"/main", MainHandler),
        (r"/logout", LogoutHandler),
        (r"/questions", QuestionsHandler),
        (r"/contest", ConstHandler),
        (r"/rank", RankHandler),
        (r"/discuss", DiscussHandler),
        (r"/admin", AdminHandler),
        (r"/judge", AdminOperationHandler),
        (r"/design", DesignHandler),
        ], cookie_secret="61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=", 
                                      static_path = os.path.join(os.path.dirname(__file__), "static"),
                                      template_path = os.path.join(os.path.dirname(__file__), "template"), 
                                      debug=True)

if __name__ == '__main__':
    tornado.options.parse_command_line()
    app = application
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()
